package cn.marinda.runnable;

import cn.marinda.entity.AbstractServerPlugin;
import cn.marinda.entity.Server;
import cn.marinda.gui.impl.DownloadServerDiaLogImpl;
import cn.marinda.util.*;
import lombok.SneakyThrows;

import javax.swing.*;
import java.util.Set;

/**
 * 下载任务线程
 *
 * @author admin
 * @date 2022/07/11
 */
public class DownloadServerPluginRunnable implements Runnable{

    private JProgressBar progressBar;
    private String pluginPath;
    private Server serverLocation;

    /**
     * 下载可运行
     *
     * @param progressBar 进度条
     */
    public DownloadServerPluginRunnable(JProgressBar progressBar, Server serverLocation, String pluginPath){
        this.progressBar = progressBar;
        this.pluginPath = pluginPath;
        this.serverLocation = serverLocation;
    }

    @SneakyThrows
    @Override
    synchronized public void run() {
        String url = pluginPath;
        if(url == null || url == ""){
            return;
        }
        HttpUtils.downloadFlag = HttpUtils.downloadPlugin(progressBar,serverLocation,url);
        if(HttpUtils.downloadFlag){
            FileUtils.moveServerPluginFiles();
            if(HttpUtils.isOnlineFlag) {
                SwingUtils.downloadDialog.close();
            }
//            清空选择项！
            ServerUtils.serverPluginSet.clear();
            ThreadUtils.downloadPluginScheduled.cancel(true);
        }
    }
}
